#博客编辑页
from time import sleep
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait

from common.Untils import BlogDriver

class BlogEdit:
    url = ""
    driver = ""

    def __init__(self):
        self.url = "http://8.137.19.140:9090/blog_edit.html"
        self.driver = BlogDriver.driver
        self.driver.get(self.url)

    #正确发布博客（登录状态下）
    def EditSucessTestByLogin(self):
        sleep(1)
        self.driver.find_element(By.CSS_SELECTOR,"#title").send_keys("自动化测试创建")
        #找到编辑区域，输入关键词（编辑区域不可操作）
        #菜单栏元素无法定位
        #博客系统编辑区域默认情况下就不为空，可以暂不处理

        #通过点击菜单栏达到编辑区域不为空的目的
        #self.driver.find_element(By.CSS_SELECTOR,"#editor > div.editormd-toolbar > div > ul > li:nth-child(21) > a > i").click()

        #直接点击发布按钮来发布博客
        self.driver.find_element(By.CSS_SELECTOR,"#submit").click()
        #actual=self.driver.find_element(By.CSS_SELECTOR,"body > div.container > div.right > div:nth-child(6) > div.title").text
        #assert actual=="自动化测试创建"
        #屏幕截图
        BlogDriver.getScreenShot()
    def EditFailTestByLogin(self):
        #回到编辑页
        self.driver.get(self.url)
        #不填写文章标题填写内容点击发布
        self.driver.find_element(By.CSS_SELECTOR, "#submit").click()
        #跳出弹窗
        alert = WebDriverWait(self.driver, 20).until(EC.alert_is_present())
        alert = self.driver.switch_to.alert
        alert.accept()

    #发布博客（未登录状态下）
    def EditFailTestByNotLogin(self):
        # 点击注销
        self.driver.find_element(By.CSS_SELECTOR, "body > div.nav > a:nth-child(6)").click()
        self.driver.get(self.url)
        self.driver.find_element(By.CSS_SELECTOR, "#title").send_keys("自动化测试创建")
        # 直接点击发布按钮来发布博客
        self.driver.find_element(By.CSS_SELECTOR, "#submit").click()
        #检查是否跳转回登录页面
        self.driver.find_element(By.CSS_SELECTOR, "#username").send_keys("zhangsan")
        self.driver.find_element(By.CSS_SELECTOR, "#password").send_keys("123456")
        self.driver.find_element(By.CSS_SELECTOR, "#submit").click()
